package com.dhcc.bpm.modules.scf.controller;

import com.dhcc.bpm.modules.scf.service.IScfFileAttachmentInfoService;
import com.dhcc.bpm.modules.scf.vo.ScfFileAttachmentInfoVO.ScfFileAttachmentInfoVO;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.support.Kv;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * 文件上传控制器
 * liuguoting
 * 2023-09-06
 */
@RestController
@AllArgsConstructor
public class ScfFileAttachmentInfoController {

    private IScfFileAttachmentInfoService scfFileAttachmentInfoService;

    /**
     * 文件上传
     *
     * @param operatorId
     * @param fileType
     * @param file
     * @return
     * @throws IOException
     * @throws ParseException
     */
    @PostMapping("/upload")
    public R<ScfFileAttachmentInfoVO> uploadAttachment(@RequestParam("operatorId") String operatorId, @RequestParam("fileType") String fileType, MultipartFile file ,@RequestParam("associationId") String associationId,@RequestParam(value = "fileCategory", required = false) String fileCategory) throws Exception {
        ScfFileAttachmentInfoVO fileAttachmentInfoVO = scfFileAttachmentInfoService.uploadAttachment(operatorId, fileType, file ,associationId,fileCategory);
        return R.data(fileAttachmentInfoVO);
    }

    /**
     * 文件预览
     * @param rowIds
     * @return
     * @throws IOException
     */
    @GetMapping("/fileDetails")
    // @Cacheable(value = "fileDetailsCache", key = "'fileDetailsCacheKey-' + #rowIds.hashCode()", unless = "#result == null")
    public R<List<Kv>> fileDetails(@RequestParam List<String> rowIds,@RequestParam (value = "fileCategory",required = false) String fileCategory) throws IOException {
        return R.data(scfFileAttachmentInfoService.fileDetails(rowIds,fileCategory));
    }

    /**
     * 文件删除
     * @param rowId
     * @return
     */
    @PostMapping("/ScfDelFile")
    public R<Boolean> deleteFileByRowId(@RequestParam String rowId){
        return scfFileAttachmentInfoService.deleteFileByRowId(rowId);
    }
}
